package com.gaoyi.crm.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.text.TextUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BitmapUtils {

    private static volatile BitmapUtils instance;

    public static BitmapUtils getInstance() {
        if (instance == null) {
            synchronized (BitmapUtils.class) {
                if (instance == null) {
                    instance = new BitmapUtils();
                }
            }
        }
        return instance;
    }

    /**
     * 压缩图片
     */
    public Bitmap resizeBitmap(String srcPath) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = true;// 开始读入图片，此时把options.inJustDecodeBounds 设回true了
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        // 主流手机分辨率：800*480
        float hh = 800f;
        float ww = 480f;
        int be = 1;// be=1表示不缩放
        // 缩放比。由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
        if (w > h && w > ww) be = (int) (w / ww);// 如果宽度大的话根据宽度固定大小缩放
        else if (w < h && h > hh) be = (int) (h / hh);// 如果高度高的话根据宽度固定大小缩放
        if (be == 1) {
            be += 1;
        }
        newOpts.inSampleSize = be;// 设置缩放比例
        newOpts.inScaled = true;// 设置图片可以缩小
        int calSize = Math.min(h, w);
        newOpts.inTargetDensity = (int) ((ww * newOpts.inDensity) / (calSize / newOpts.inSampleSize));
        newOpts.inJustDecodeBounds = false; //重新读入图片，注意此时已经把options.inJustDecodeBounds 设回false了
        bitmap = BitmapFactory.decodeFile(srcPath,newOpts);
        return bitmap;
    }

    /**
     * 旋转Bitmap
     *
     * @param angle 旋转角度
     */
    public Bitmap rotatingBitmap(Bitmap targetBitmap, int angle) {
        if (targetBitmap == null) return null;
        Bitmap bmp = null;
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        try {
            bmp = Bitmap.createBitmap(targetBitmap, 0, 0, targetBitmap.getWidth(), targetBitmap.getHeight(), matrix, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (bmp == null) bmp = targetBitmap;
        targetBitmap.recycle();
        return bmp;
    }

    public String bitmap2Url(Activity activity,Bitmap bitmap, String url){
        FileOutputStream fops = null;
        if (TextUtils.isEmpty(url)) {
            File photoFile = new File(activity.getExternalCacheDir(), PhotoUtils.getInstance().getImgName());
            url = photoFile.getAbsolutePath();
        }
        try {
            fops = new FileOutputStream(url);
            bitmap.compress(Bitmap.CompressFormat.JPEG,80,fops);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                if (fops != null) {
                    fops.flush();
                    fops.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return url;
    }
}
